const api = require('../../api/api.js');

Page({
  data: {
    localImgSrc: '',
    entArr: [],
    entIdArr: [],
    currentIndex: 0
  },
  onLoad() {
    let that = this;
    wx.getStorage({
      key: 'userId',
      success(e) {
        console.log(e);
        that.setData({
          userId: e.data
        });
      }
    })
    wx.getStorage({
      key: 'role',
      success(res) {
        that.setData({
          role: res.data
        })
        const role = res.data
        if (role === 0) {
          that.getEntAdmin()
        } else {
          that.getEnt()
        }
      }
    })
  },
  getEntAdmin() {
    let that = this
    api.getAllEnterprise({
      data: {
        page: -1,
        limit: -1
      },
      success(response) {
        that.formatEntData(response.data)
      }
    })
  },
  getEnt() {
    let that = this
    if (that.data.role === 4) {
      wx.getStorage({
        key: 'userId',
        success(res) {
          api.queryEnterprise({
            data: {
              account: res.data,
              page: -1,
              limit: -1
            },
            success(response) {
              that.formatEntData(response.data)
            }
          })
        }
      })
    } else {
      wx.getStorage({
        key: 'userId',
        success(res) {
          api.queryPlantation({
            data: {
              account: res.data
            },
            success(response) {
              that.formatEntData(response.data)
            }
          })
        }
      })
    }
  },
  formatEntData(data) {
    let that = this
    let entArr = []
    let entIdArr = []
    data.enterprise.map(item => {
      entArr.push(item.entName)
      entIdArr.push(item.entId)
    })
    if (entArr.length === 0) {
      entArr = ['无所属企业！'];
      entIdArr = [-1];
    }
    that.setData({
      entArr,
      entIdArr
    })
  },
  handleChange(e) {
    this.setData({
      currentIndex: Number.parseInt(e.detail.value)
    });
  },
  formSubmit(e) {
    let that = this;
    let name = e.detail.value.name.trim();
    let remark = e.detail.value.remark.trim();
    let userId = that.data.userId;
    let entId = that.data.entIdArr[that.data.currentIndex];
    let imgUrl = that.data.localImgSrc;
    if (name === '') {
      wx.showToast({
        title: '请将仓库名填写完整！',
        icon: 'none'
      })
      return;
    }
    if (remark === '') {
      remark = '无';
      return;
    }
    if (imgUrl === '') {
      wx.showToast({
        title: '请选择图片！',
        icon: 'none'
      })
      return;
    }
    wx.showLoading({
      title: '提交中...',
    });
    api.addFile({
      data: {
        userId: userId,
        path: 'warehouseImage',
        filePath: imgUrl
      },
      success(res) {
        let image = JSON.parse(res.data).fileName;
        api.addWarehouse({
          data: {
            name,
            remark,
            image,
            videoUrl: '',
            entId,
            userId
          },
          success(res) {
            if (res.data === true) {
              let pages = getCurrentPages();
              let prePage = pages[pages.length - 2];
              prePage.onLoad();
              that.formitSuccess();
            }
          }
        });
      }
    });
  },
  formitSuccess() {
    wx.hideLoading();
    wx.showToast({
      title: '提交成功！',
    });
    wx.navigateBack({
      delta: '-1'
    });
  },
  pickimg(e) {
    let that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        that.setData({
          localImgSrc: res.tempFilePaths[0]
        });
      }
    })
  },
  delimg() {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '确定取消选中的图片吗？',
      success(e) {
        if (e.confirm) {
          that.setData({
            localImgSrc: ''
          });
        }
      }
    })
  }
})